package com.ac.scum.log.web.model.wechat.mp.controller;

import com.ac.scum.log.web.model.wechat.mp.service.AuthUserService;
import com.ac.scum.wechat.bean.WeChatUser;
import jakarta.servlet.http.HttpServletResponse;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.error.WxErrorException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

/**
 * 微信授权回调
 */
@Slf4j
@Controller
@RequestMapping("/wechat/redirect")
@AllArgsConstructor
public class WeChatRedirectController {
    private final AuthUserService authUserService;

    @RequestMapping("/greet")
    public String greetUser(@RequestParam String code) {
        try {
            WeChatUser weChatUser = authUserService.parseCode(code);
            return "redirect:http://next.scum-log.cn/wechath5?openid=" + weChatUser.getOpenId();
        } catch (WxErrorException e) {
            log.error(e.getMessage(), e);
        }
        return "redirect:http://next.scum-log.cn/wechath5?openid=null";
    }
}
